package pressuretest;

import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import org.json.JSONObject;
import api.OrderApi;
import api.PurseApi;
import good.Good;
import good.Item;

/**
 * 下订单操作
 * 
 * @author 高文
 *
 */
public class Order
{
	/**
	 * 下订单
	 * @param sid 服务器id
	 * @param uid 下单用户id
	 * @param rid 下单角色id
	 * @param rname 下单角色名
	 * @param good 商品
	 * @param level 价格等级
	 * @return 成功或调用失败原因
	 */
	public static String MakeOrder(int sid, int uid, BigInteger rid, String rname,String good, int level)
	{
		String testResult = "成功";
		try
		{
			ArrayList<Item> itmList = new ArrayList<>();
			itmList.add(new Item(new Good(new JSONObject(good)), level, 1));
			OrderApi.shopOrder(1, sid, uid, rid, rname, uid, rid, rname, Instant.now().getEpochSecond(), itmList , "1.1.1.1");
			int i = 300;
			while(i>0)
			{
				String result = OrderApi.orderResult(sid);
				System.out.println(result);
				if (result==null || result.isEmpty())
				{
					Thread.sleep(1000);
					i--;
				}
				else {
					OrderApi.orderResultOK(new JSONObject(result).getString("oid"), 1, Instant.now().getEpochSecond());
					PurseApi.msQueueResult(sid);
					break;
				}
			}
		}
		catch (Exception e)
		{
			return e.getMessage();
		}
		return testResult;
	}

	public static void main(String[] args) throws Exception
	{
		String string = MakeOrder(889901, 123977, BigInteger.valueOf(112233),"gao123","{\"GoodsID\":102,\"MallID\":1,\"MallName\":\"在水一方\",\"ItemID\":\"1_15955\",\"customID\":955,\"sKlass\":1,\"Klass\":-1,\"sortID\":\"0;1#1;1\",\"IsMark\":52,\"MallIcon\":\"female_wz601_body\",\"RelateGoodsID\":\"101\",\"MallTips\":\"\",\"RestrictionID\":0,\"MinBuyCount\":1,\"MaxBuyCount\":1,\"CanPresent\":1,\"BuyMoneyType\":1,\"BuyMoneyNum4\":2580,\"BuyMoneyDescr4\":\"\",\"DiscountPrice4\":2270,\"BuyMoneyNum3\":-1,\"BuyMoneyDescr3\":\"\",\"DiscountPrice3\":-1,\"BuyMoneyNum2\":-1,\"BuyMoneyDescr2\":\"\",\"DiscountPrice2\":-1,\"BuyMoneyNum1\":-1,\"BuyMoneyDescr1\":\"\",\"DiscountPrice1\":-1,\"FreeGiftItemID\":\"\",\"Limit\":-1}", 4);
		System.out.println(string);
	}
}
